﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SettlersEngine
{
    internal struct AnimationTime
    {
        private Int64 m_Cycles;

        public TimeSpan Time { 
            get { return TimeSpan.FromMilliseconds(m_Cycles * RuntimeMap.CYCLE_MILLIS); }
            set
            {
                m_Cycles = (long)(value.TotalMilliseconds / RuntimeMap.CYCLE_MILLIS);
            }
        }
        public Int64 CycleCount
        {
            get { return m_Cycles; }
            set
            {
                m_Cycles = value;
            }
        }

        public AnimationTime(Int64 inCycleCount)
        {
            m_Cycles = inCycleCount;
        }

        public AnimationTime(TimeSpan inTime)
        {
            m_Cycles = 0;

            Time = inTime;
        }

        public AnimationTime AddOneCycle()
        {
            return new AnimationTime(m_Cycles + 1);
        }
    }

    internal partial class RuntimeMap
    {
        public void Process(object sender, EventArgs args)
        {
            Int64 targetCycleCount = new AnimationTime(m_Watch.Elapsed).CycleCount;

            /*
             * MULTIPLAYER SYNCHRONIZATION:
             * 
             * Server sends pings with latest information on a X ms boundary. The game must pause if no information
             * arrive within this boundary. If a player receives the packet with delay, his game will pause first,
             * but as soon as the server discovers, that one player didn't receive the packet, no further packets are
             * sent to any player until situation clears. The server may further provide all players with state information,
             * like which player is disconnected, etc. The players may sent information to server at any time but all their
             * action have at least the X ms delay.
             * 
             * The X ms boundary should be dynamic, depending on connection quality of all players. This is done by measuring
             * the ping on server-side and setting X to an appropiate value.
             * 
             * This kind of synchronization allows to minimize traffic, since only user actions have to be transmitted.
             */

            while(AnimationTime.CycleCount < targetCycleCount)
            {
                AnimationTime = AnimationTime.AddOneCycle();

                Grid.AddOneCycle();

                if ((AnimationTime.CycleCount % (1000 / CYCLE_MILLIS)) == 0)
                    ProcessBuildingProduction();

                UpdateScreen();

                if (m_HasBuildSpaceChanged)
                {
                    if (OnBuildSpaceChanged != null)
                        OnBuildSpaceChanged(this);

                    m_HasBuildSpaceChanged = false;
                }
            }
        }
    }
}
